package com.example.zhongxiangmo;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class DuoBuJuFragment extends Fragment {


    private View view;
    private RecyclerView mRlv;
    private String sUrl = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
    private ArrayList<DuoBuBean.DataBean> list;
    private DuoBuJuAdapter duoBuJuAdapter;
    private ProgressBar mPb;

    public DuoBuJuFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_duo_bu_ju, container, false);
        initView(inflate);
        initListener();
        return inflate;
    }

    private void initListener() {
        duoBuJuAdapter.setItemClickListener(new DuoBuJuAdapter.ItemClickListener() {
            @Override
            public void ItemClick(int i) {
                Toast.makeText(getActivity(), "哈哈哈哈", Toast.LENGTH_SHORT).show();
            }
        });

        duoBuJuAdapter.setItemLongClickListener(new DuoBuJuAdapter.ItemLongClickListener() {
            @Override
            public void ItemLongClick(int i) {
                i = i;
            }
        });


    }

    private void initView(View inflate) {
        mRlv = (RecyclerView) inflate.findViewById(R.id.rlv);
        mPb = (ProgressBar) inflate.findViewById(R.id.pb);

        list = new ArrayList<>();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRlv.setLayoutManager(linearLayoutManager);

        duoBuJuAdapter = new DuoBuJuAdapter(getActivity(), list);

        mRlv.setAdapter(duoBuJuAdapter);

        initData();

        registerForContextMenu(mRlv);
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(sUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    int code = connection.getResponseCode();
                    if (code == HttpURLConnection.HTTP_OK) {
                        InputStream is = connection.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String len = null;
                        StringBuffer sbf = new StringBuffer();
                        while ((len = br.readLine()) != null) {
                            sbf.append(len);
                        }
                        is.close();
                        br.close();
                        String json = sbf.toString();
                        Gson gson = new Gson();
                        DuoBuBean duoBuBean = gson.fromJson(json, DuoBuBean.class);
                        final List<DuoBuBean.DataBean> data = duoBuBean.getData();

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                list.addAll(data);
                                duoBuJuAdapter.notifyDataSetChanged();
                                mPb.setVisibility(View.GONE);
                            }
                        });

                    }


                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0, 0, 0, "详情");
        menu.add(0, 0, 0, "删除");
        menu.add(0, 0, 0, "修改");

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();

                break;
            case 2:
                Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();

                break;
        }

        return super.onContextItemSelected(item);
    }
}
